using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Data;
using MacomberMap.Common.Components;
using MacomberMap.Common.Internals;
using MacomberMap.Common.User_Interfaces.Components;
using MacomberMap.Common.Types;

namespace MacomberMap.Common.Database_Interface
{
    /// <summary>
    /// This class allows the MM loader to retrieve a savecase
    /// </summary>
    public class MM_Database_Savecase: MM_Database_Oracle
    {
        #region Variable declarations
        /// <summary>The XML document for the savecase</summary>
        private XmlDocument xDoc = new XmlDocument();
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new savecase
        /// </summary>
        /// <param name="Filename"></param>
        public MM_Database_Savecase(String Filename)
            : base("","","","Savecase")
        {
            xDoc.Load(Filename);
        }
        #endregion

        /// <summary>
        /// Open the savecase database (nothing to do)
        /// </summary>
        public override void Open()
        {
        }

        /// <summary>
        /// Close the savecase database
        /// </summary>
        public override void Close()
        {
            xDoc = null;            
        }

        /// <summary>
        /// Report on the savecase state
        /// </summary>
        public override System.Data.ConnectionState State
        {
            get
            {
                if (xDoc == null)
                    return System.Data.ConnectionState.Broken;
                else
                    return System.Data.ConnectionState.Open;
            }
        }

        /// <summary>
        /// Report on the type of database
        /// </summary>
        public override string Type
        {
            get { return "Savecase"; }
            set
            {
                if (value != "Savecase")
                    throw new InvalidDataException("An incorrect database type was passed to the Savecase database initializer: " + value + "!");
            }
        }

        /// <summary>
        /// Report the stats on the database connection
        /// </summary>
        /// <returns></returns>
        public override System.Data.DataTable ConnectionStats()
        {
            DataTable OutTable = new DataTable();
            OutTable.Columns.Add("Element", typeof(string));
            OutTable.Columns.Add("Count", typeof(int));
            Dictionary<String, int> ConnStats = new Dictionary<string, int>();
            foreach (XmlElement xElem in xDoc.SelectNodes("//*"))
                if (ConnStats.ContainsKey(xElem.Name))
                    ConnStats[xElem.Name]++;
                else
                    ConnStats.Add(xElem.Name, 1);
            foreach (KeyValuePair<string, int> kvp in ConnStats)
                OutTable.Rows.Add(kvp.Key, kvp.Value);
            return OutTable;
        }

        /// <summary>
        /// Load companies in
        /// </summary>
        public override void LoadCompanies()
        {
            foreach (XmlElement xElem in xDoc.SelectNodes("//Company"))
            {
                MM_Company NewCompany = new MM_Company(xElem);
                MM_Repository.Companies.Add(NewCompany.Name, NewCompany);
                MM_Repository.TEIDs.Add(NewCompany.TEID, NewCompany);
            }
        }

        /// <summary>
        /// Load counties
        /// </summary>
        public override void LoadCounties()
        {
            foreach (XmlElement xElem in xDoc.SelectNodes("//County"))
                new MM_Boundary(xElem);            
        }

        /// <summary>
        /// Save state coordinates - do nothing
        /// </summary>
        public override void SaveStateCoordinates()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Save counties - do nothing
        /// </summary>
        public override void SaveCounties()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Save a boundary - do nothing
        /// </summary>
        /// <param name="Boundary"></param>
        public override void SaveBoundary(MM_Boundary Boundary)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load lines
        /// </summary>
        public override void LoadLines()
        {
            foreach (XmlElement xElem in xDoc.SelectNodes("//Line"))
            {
                MM_Line NewLine = new MM_Line(xElem);
                if (!MM_Repository.TEIDs.ContainsKey(NewLine.TEID))
                    MM_Repository.TEIDs.Add(NewLine.TEID, NewLine);
                if (!MM_Repository.Lines.ContainsKey(NewLine.Name))
                    MM_Repository.Lines.Add(NewLine.Name, NewLine);
            }            
        }

        /// <summary>
        /// Merge DC ties
        /// </summary>
        public override void MergeDCTies()
        {
            
        }

        /// <summary>
        /// Load substations
        /// </summary>
        public override void LoadSubstations()
        {
            foreach (XmlElement xElem in xDoc.SelectNodes("NetworkModel/Substation"))
            {
                MM_Substation NewSub = new MM_Substation(xElem);
                if (!MM_Repository.TEIDs.ContainsKey(NewSub.TEID))
                    MM_Repository.TEIDs.Add(NewSub.TEID, NewSub);
                if (!MM_Repository.Substations.ContainsKey(NewSub.Name))
                    MM_Repository.Substations.Add(NewSub.Name, NewSub);
            }
        }

        /// <summary>
        /// Load in KV levels
        /// </summary>
        public override void LoadKVLevels()
        {
            foreach (XmlElement xElem in xDoc.SelectNodes("//VoltageLevel"))
                MM_Overall_Display.KVLevels.Add(xElem.Attributes["Name"].Value, new MacomberMap.Common.Types.MM_KVLevel(xElem));            
        }

        /// <summary>
        /// Save substation coordinates
        /// </summary>
        public override void SaveSubstationCoordinates()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Blank the database
        /// </summary>
        public override void BlankDatabase()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Create new tables
        /// </summary>
        /// <param name="DbConnection"></param>
        public override void CreateTables(object DbConnection)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Save county websites
        /// </summary>
        /// <param name="Filename"></param>
        public override void SaveCountyWebsites(string Filename)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Save configuration parameters
        /// </summary>
        /// <param name="xW"></param>
        public override void SaveConfigurationParameters(System.Xml.XmlTextWriter xW)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Save a map tile
        /// </summary>
        /// <param name="NewTile"></param>
        public override void SaveMapTile(MacomberMap.Common.Components.MM_MapTile NewTile)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load a map tile
        /// </summary>
        /// <param name="mapType"></param>
        /// <param name="TileXY"></param>
        /// <param name="ZoomLevel"></param>
        /// <param name="Tile"></param>
        /// <returns></returns>
        public override bool LoadMapTile(MacomberMap.Common.Components.MM_MapTile.MapType mapType, System.Drawing.Point TileXY, int ZoomLevel, out System.Drawing.Bitmap Tile)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Lookup a user
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public override int LookupUser(string UserName)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load a role
        /// </summary>
        /// <param name="UserRole"></param>
        /// <param name="DefaultView"></param>
        /// <param name="PermissionsXML"></param>
        public override void LoadRole(int UserRole, out int DefaultView, out string PermissionsXML)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load zones
        /// </summary>
        public override void LoadZones()
        {
        }

        /// <summary>
        /// Load users and roles
        /// </summary>
        /// <returns></returns>
        public override System.Data.DataSet LoadUsersAndRoles()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load element types
        /// </summary>
        public override void LoadElementTypes()
        {
            foreach (XmlElement xElem in xDoc.SelectNodes("//ElementType"))
                MM_Overall_Display.ElemTypes.Add(xElem.Attributes["Name"].Value, new MM_Element_Type(xElem));            
        }


        /// <summary>
        /// Load event types
        /// </summary>
        public override void LoadEventTypes()
        {
            foreach (XmlElement xElem in xDoc.SelectNodes("//Violation"))
                MM_Overall_Display.EventTypes.Add(xElem.Attributes["Name"].Value, new MM_Event_Type(xElem));            
        }

        /// <summary>
        /// Load views
        /// </summary>
        /// <param name="UserRole"></param>
        /// <param name="DefaultView"></param>
        public override void LoadViews(int UserRole, int DefaultView)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load one-lines
        /// </summary>
        /// <returns></returns>
        public override System.Data.DataTable LoadOneLines()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load a one-line
        /// </summary>
        /// <param name="TEID"></param>
        /// <returns></returns>
        public override string LoadOneLine(ulong TEID)
        {
            throw new Exception("The method or operation is not implemented.");
        }


        /// <summary>
        /// Load a one-line
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public override string LoadOneLine(string Name)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Save a one-line
        /// </summary>
        /// <param name="BaseElement"></param>
        /// <param name="OneLineXML"></param>
        public override void SaveOneLine(MacomberMap.Common.Components.MM_Element BaseElement, string OneLineXML)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Locate an element
        /// </summary>
        /// <param name="TEID"></param>
        /// <returns></returns>
        public override MacomberMap.Common.Components.MM_Element LocateElement(ulong TEID)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Locate an element
        /// </summary>
        /// <param name="TEID"></param>
        /// <param name="ElemType"></param>
        /// <returns></returns>
        public override MacomberMap.Common.Components.MM_Element LocateElement(ulong TEID, MacomberMap.Common.Types.MM_Element_Type ElemType)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Locate an element
        /// </summary>
        /// <param name="SubstationName"></param>
        /// <param name="Name"></param>
        /// <param name="ElemType"></param>
        /// <returns></returns>
        public override MacomberMap.Common.Components.MM_Element LocateElement(string SubstationName, string Name, MacomberMap.Common.Types.MM_Element_Type ElemType)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Load all elements within a base element
        /// </summary>
        /// <param name="BaseElement"></param>
        /// <returns></returns>
        public override List<MacomberMap.Common.Components.MM_Element> LoadElements(MacomberMap.Common.Components.MM_Element BaseElement)
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
}
